﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Moon.DBUtility;

namespace DAL
{
    public class ActionDAL
    {
        /// <summary>
        /// 根据所给字符获得相关信息
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="column">列名</param>
        /// <param name="wherestr">查询条件</param>
        /// <returns></returns>
        public DataTable FindByStr(string table, string[] column, string wherestr)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select ");
            if (column != null && column.Length > 0)
            {
                foreach (string tmp in column)
                {
                    sb.Append(tmp + ",");
                }
                sb.Remove(sb.Length - 1, 1);
            }
            else
            {
                sb.Append(" * ");
            }
            sb.Append(" from ");
            sb.Append(table);
            if(!"".Equals(wherestr))
            {
                sb.Append(" where ");
                sb.Append(wherestr);
            }
            return DbHelperOleDb.Query(sb.ToString()).Tables[0];
        }
        /// <summary>
        /// 获取给定条件的信息总数
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public int GetCountByStr(string table, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM "+table);
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页方法
        /// </summary>
        /// <param name="Star">开始位置</param>
        /// <param name="End">结束位置</param>
        /// <param name="table">表名称</param>
        /// <param name="wherestr">查询条件</param>
        /// <param name="Orderby">排序字段</param>
        /// <returns></returns>
        public DataTable PageByStr(int Star, int End, string table, string strWhere, string Orderby)
        {
            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("select top " + Star + " from " + table);
            if (strWhere.Trim() != "")
            {
                strSql1.Append(" where " + strWhere);
            }
            if (Orderby != null && Orderby.Length > 0)
            {
                strSql1.AppendFormat(" order by {0} Desc", Orderby);
            }
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("select top " + End + " from (" + strSql1.ToString()+ ")");
            if (Orderby != null && Orderby.Length > 0)
            {
                strSql2.AppendFormat(" order by {0} ", Orderby);
            }
            return DbHelperOleDb.Query(strSql2.ToString()).Tables[0];
        }
    }
}
